//
//  AppDelegate.swift
//  SinaBlog
//
//  Created by 李超 on 16/3/19.
//  Copyright © 2016年 lichao. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //获取用户的登陆信息，以便根据登陆信息来进行控制器之间的跳转
//       let account = CZUserAccountTool.shareInstance.userAccount
//        if account != nil{
//            print("用户已登录过")
//        }
        //手动添加window
        window = UIWindow(frame:UIScreen.mainScreen().bounds)
        //设置根控制器
        //let TabBarVC = TabBarController()
        //测试新特性控制器
       // let newFeatureVC = NewFeatureController()
        //测试欢迎界面
        //let welcomeVC = WelcomeController()
        
        window?.rootViewController = defaultViewController()
        //是窗口变成主窗口并显示
        window?.makeKeyAndVisible()
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()        
        return true
    }
    
    //根据是否有新特性以及是否为首次登陆来进行选择跟控制器
    private func defaultViewController() -> UIViewController?{
        let account = CZUserAccountTool.shareInstance.userAccount
        if (account == nil) {
            return TabBarController()
        }
        
        return isNewVesion() ? NewFeatureController() : WelcomeController()
    }
    
    func isNewVesion() -> Bool{
        let currentVesion  = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let sandBoxKey = "sandBoxKey"
        let sandBoxVesion = NSUserDefaults.standardUserDefaults().stringForKey(sandBoxKey)
        let newVesion = currentVesion != sandBoxVesion
        //如果是新版本则进行保存
        if newVesion{
            NSUserDefaults.standardUserDefaults().setValue(currentVesion, forKeyPath: sandBoxKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        return newVesion
    }
    
    class func outSwitchViewController(isTabBar:Bool){
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(isTabBar)
    }
    //外部调用起来还是比较麻烦，需要再次进行包装
   private func switchViewController(isTabBar:Bool){
        
        let rootVC = isTabBar ? TabBarController() : WelcomeController()
        window?.rootViewController = rootVC
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

